package SolodkovDA.pages;

import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import SolodkovDA.model.Film;

public class FilmListPage extends InternalPage{

	public FilmListPage(PageManager pages) {
		super(pages);
	}
	
	 @FindBy(css = "nav a[href $= '?go=add']")
	  private WebElement add_movieButton;
	 
	 @FindBy(id = "q")
	  private WebElement search_movieBox;
	 
	  public FilmListPage clickAddMovieLink() {
		  	add_movieButton.click();
		    return pages.filmListPage;
		  }
	  
	  public FilmListPage startSearch(String searchText) {
		  search_movieBox.clear();
		  search_movieBox.sendKeys(searchText + Keys.RETURN);
		  return pages.filmListPage;
		  }
	  
	  public boolean checkNegativeSearchResults(){
		  wait.until(ExpectedConditions.textToBePresentInElement(By.cssSelector("div.content"), "No movies where found."));
		  return true;
	  }
	  
	  public boolean checkPositiveSearchResults(String searchText){
		  wait.until(ExpectedConditions.textToBePresentInElement(By.cssSelector("div.title"), searchText));
		  return true;
	  }
	  
	  public FilmListPage ensurePageLoaded() {
		    super.ensurePageLoaded();
		    wait.until(presenceOfElementLocated(By.id("q")));
		    return this;
		  }
	
	  public boolean openFilmEditPage(String title){
		  List<WebElement> elements = driver.findElements(By.cssSelector("div.title"));
		  for (int i = 0; i < elements.size(); i++)
		  {
	          if( elements.get(i).getText().equals(title)) 
	          {
	        	  elements.get(i).click(); 
	        	  return true;
	          }
	        }
		  return false;
	  }
}
